/*
 * Powered By www.cardmee.net
 */
package com.ns.school.controller.wave;

import com.ns.school.po.edu.LayuiResult;
import com.ns.school.po.sys.User;
import com.ns.school.po.wave.WaveDevicePO;
import com.ns.school.po.wave.WaveRequestDetailPO;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import com.ns.school.common.constant.StaticMsg;
import com.ns.school.controller.base.BaseController;
import com.ns.school.po.wave.WaveOrderRecordPO;
import com.ns.school.common.base.R;
import com.ns.school.service.wave.WaveOrderRecordService;
import com.ns.school.common.exception.MyException;
import com.ns.school.common.config.SecurityUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wt-97
 */
@Slf4j
@Controller
@RequestMapping("order")
public class WaveOrderRecordController extends BaseController {

    @Autowired
    private WaveOrderRecordService waveOrderRecordService;


    @RequestMapping("list")
    public String list(Model model) {
        return "view/order/list";
    }

    @RequestMapping("add")
    public String add(Model model, ModelMap modelMap ){
        Map<String, Object> map = new HashMap<>();
        List<WaveDevicePO> deviceList =waveOrderRecordService.queryAllName(map);
        List<User> userList =waveOrderRecordService.selectAllNames(map);
        modelMap.put("device", deviceList);
        modelMap.put("user", userList);
        return "view/order/add";
    }

    //编辑
    @RequestMapping("edit")
    public String edit(Model model, Integer id, ModelMap modelMap){
        Map<String, Object> map = new HashMap<>();
        List<WaveDevicePO> deviceList = waveOrderRecordService.queryAllName(map);
        WaveOrderRecordPO waveOrderRecordPO= waveOrderRecordService.selectWaveOrder(id);// 此处需要通过参数id获取到具体的对象
        List<User> userList = waveOrderRecordService.selectAllNames(map);
        waveOrderRecordPO.setEncryptionId(encode(waveOrderRecordPO.getId()));
        modelMap.put("waveOrder",waveOrderRecordPO);
        modelMap.put("device", deviceList);
        modelMap.put("user", userList);
        return "view/order/edit";
    }

    /**
     * 查询列表
     *
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "getWaveOrderList")
    @ResponseBody
    public R<List<?>> list(HttpServletRequest request, WaveOrderRecordPO params) {
        try {
            return waveOrderRecordService.queryPageList(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 查询单个详情
     *
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "getWaveOrderRecordInfo")
    public R<?> info(HttpServletRequest request, WaveOrderRecordPO params) {
        try {
            if (isEmpty(params.getEncryptionId())) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            Integer id = decode(params.getEncryptionId());
            if (id == null) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            params.setId(id);
            return R.success(waveOrderRecordService.queryById(params));
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 添加
     *
     * @param request
     * @param params
     * @return
     */
    @PostMapping("insertWaveOrderRecord")
    @ResponseBody
    public R<?> insert(HttpServletRequest request,@RequestBody WaveOrderRecordPO params) {
        try {
            Integer userId = SecurityUtil.id();
            params.setCreateBy(userId);
            return waveOrderRecordService.insert(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 更新
     *
     * @param request
     * @param params
     * @return
     */
    @PostMapping("updateWaveOrderRecord")
    @ResponseBody
    public R<?> update(HttpServletRequest request,@RequestBody WaveOrderRecordPO params) {
        try {
            if (isEmpty(params.getEncryptionId())) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            Integer id = decode(params.getEncryptionId());
            if (id == null) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            params.setId(id);
            Integer userId = SecurityUtil.id();
            params.setUpdateBy(userId);
            return waveOrderRecordService.update(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 删除
     *
     * @param request
     * @param params
     * @return
     */
    @PostMapping("updateDelWaveOrderRecord")
    @ResponseBody
    public R<?> updateDel(HttpServletRequest request, WaveOrderRecordPO params) {
        return waveOrderRecordService.updateDel(params);
    }


    //批量删除
    @PostMapping("updateOrderDel")
    @ResponseBody
    public R<?> updateDelAll(WaveOrderRecordPO params, String ids) {
        return waveOrderRecordService.updateDelAll(params, ids);

    }

    //采购
    @PostMapping("orderReview")
    @ResponseBody
    public LayuiResult<WaveOrderRecordPO> auditBorrow(WaveOrderRecordPO waveOrderRecordPO) {
        LayuiResult result = new LayuiResult<>();
        int count =waveOrderRecordService.checkOrder(waveOrderRecordPO);
        if (count > 0) {
            result.setCode(500);
            result.setMsg("该审核记录已处理，请勿重复审核。");
            return result;
        } else {
            // 执行审核操作
            //waveRequestDetailPO.setId(reviewId);
            waveOrderRecordService.auditOrder(result,waveOrderRecordPO.getId());
            return result;
        }
    }

    //采购拒绝
    @PostMapping("orderReviewFail")
    @ResponseBody
    public LayuiResult<WaveOrderRecordPO> auditRequestFail(WaveOrderRecordPO waveOrderRecordPO) {
        LayuiResult result = new LayuiResult<>();
        int count =waveOrderRecordService.orderRequestFail(waveOrderRecordPO.getId());
        if (count > 0) {
            result.setCode(0);
            result.setMsg("审核拒绝");
            return result;
        } else {
            result.setCode(500);
            result.setMsg("审核错误");
            return result;
        }

    }

}
